﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using Hxsoft.AppV1.Module;

namespace Hxsoft.AppV1.WebUI
{
	/// <summary>
	/// 评论分页列表。
	/// </summary>
	public class CommentPagingList : CommentList
	{
		#region
		/// <summary>
		/// 列表分页查询字段名。
		/// </summary>
		public virtual string PageField
		{
			get { return GenericUrlManager.Comment_Query_PageField; }
		}

		/// <summary>
		/// 列表Url模式。
		/// </summary>
		public virtual string ListUrlPattern
		{
			get { return GenericUrlManager.BuildCommentListUrlPattern(InfoType, InfoId); }
		}
		#endregion

		/// <summary>
		/// 列表分页控件。
		/// </summary>
		protected Wuqi.Webdiyer.AspNetPager CtrPager;

		private LoadedValue<string> loadedInfoType;
		/// <summary>
		/// 检索列表的信息类别。
		/// </summary>
		public override string InfoType
		{
			get
			{
				if (loadedInfoType.Loader == null)
					loadedInfoType.Loader = () => Request.QueryString[GenericUrlManager.Comment_Query_InfoTypeField];
				return loadedInfoType.Value;
			}
		}

		private LoadedValue<string> loadedInfoId;
		/// <summary>
		/// 检索列表的信息标识。
		/// </summary>
		public override string InfoId
		{
			get
			{
				if (loadedInfoId.Loader == null)
					loadedInfoId.Loader = () => Request.QueryString[GenericUrlManager.Comment_Query_InfoIdField];
				return loadedInfoId.Value;
			}
		}

		private LoadedValue<int> loadedCurrentPage;
		/// <summary>
		/// 列表的当前页码。
		/// </summary>
		public virtual int CurrentPage
		{
			get
			{
				if (loadedCurrentPage.Loader == null)
					loadedCurrentPage.Loader = () => IntExtensions.ParseOrDefault(Request.QueryString[PageField], 1);
				return loadedCurrentPage.Value;
			}
		}

		private LoadedValue<SearchType> loadedSearchType;
		/// <summary>
		/// 列表的检索类型。
		/// </summary>
		public virtual SearchType SearchType
		{
			get
			{
				if (loadedSearchType.Loader == null)
					loadedSearchType.Loader = () => EnumExtensions.ParseOrDefault<SearchType>(Request.QueryString["SearchType"], SearchType.None);
				return loadedSearchType.Value;
			}
		}




		/// <summary>
		/// 列表初始化。
		/// </summary>
		/// <param name="e">事件参数。</param>
		/// <remarks>同 <seealso cref="BuyInfoPagingList"/>。</remarks>
		protected override void OnInit(EventArgs e)
		{
			CtrPager.UrlPageIndexName = GenericUrlManager.Comment_Query_PageField;
			if (SearchType != SearchType.None)
			{
				CtrPager.EnableUrlRewriting = false;
			}
			else
			{
				CtrPager.EnableUrlRewriting = true;
				CtrPager.UrlRewritePattern = ListUrlPattern;
			}
			base.OnInit(e);
		}

		/// <summary>
		/// 获取列表数据。
		/// </summary>
		/// <returns>列表数据。</returns>
		public override object GetListData()
		{
			PaginationInfo<List<Comments.CommentForClientList>> pageInfo = Comments.GetPaginationList(SiteId, InfoType, InfoId, CtrPager.PageSize, CurrentPage);
			CtrPager.RecordCount = pageInfo.RecordCount;
			CtrPager.CustomInfoHTML = GenericHelper.GetPagerCustomInfoHtml<List<Comments.CommentForClientList>>(pageInfo);
			return pageInfo.Records;
		}

		protected virtual void RewriteMeta()
		{
			this.Page.Title = Res.CommentList +
				(CurrentPage > 1 ? Hxsoft.AppV1.Page.TitleSectionSeparator + string.Format(Res.TitlePaging, CurrentPage) : "")
				+ Hxsoft.AppV1.Page.TitleSuffix;
		}

		protected override void OnPreRender(EventArgs e)
		{
			RewriteMeta();

			base.OnPreRender(e);
		}
	}
}
